package Server.Jsmj.RGCP;

import java.util.Random;

class MajPaiQiang {
	public int CountArray(int by[]) {
		return by.length;
	}

	public int sizeof(int by[]) {
		return by.length;
	}

	public void CopyMemory(int[] by, int[] by2, int length) {
		for (int i = 0; i < length; i++) {
			by[i] = by2[i];
		}
	}

	public void memcpy(int[] by, int[] by2, int length) {
		for (int i = 0; i < length; i++) {
			by[i] = by2[i];
		}
	}

	public Random rd = new Random();

	public int rand() {
		return rd.nextInt(1000);
	}

	int max_card_num = 125;
	int m_byFetchPos;
	int m_byMjPai[] = new int[max_card_num];

	int m_cbCardDataArray[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, // 0-8 һ��-����
			9, 10, 11, 12, 13, 14, 15, 16, 17, // 9-17 һͲ-��Ͳ
			18, 19, 20, 21, 22, 23, 24, 25, 26, // 18-26 һ��-����
			27, 28, 29, // �컨 ǧ�� �׻�

			0, 1, 2, 3, 4, 5, 6, 7, 8, // 0-8 һ��-����
			9, 10, 11, 12, 13, 14, 15, 16, 17, // 9-17 һͲ-��Ͳ
			18, 19, 20, 21, 22, 23, 24, 25, 26, // 18-26 һ��-����
			27, 28, 29, // �컨 ǧ�� �׻�

			0, 1, 2, 3, 4, 5, 6, 7, 8, // 0-8 һ��-����
			9, 10, 11, 12, 13, 14, 15, 16, 17, // 9-17 һͲ-��Ͳ
			18, 19, 20, 21, 22, 23, 24, 25, 26, // 18-26 һ��-����
			27, 28, 29, // �컨 ǧ�� �׻�

			0, 1, 2, 3, 4, 5, 6, 7, 8, // 0-8 һ��-����
			9, 10, 11, 12, 13, 14, 15, 16, 17, // 9-17 һͲ-��Ͳ
			18, 19, 20, 21, 22, 23, 24, 25, 26, // 18-26 һ��-����
			27, 28, 29, // �컨 ǧ�� �׻�
			34, 35, 36, 37, 38// 5�Ż��� ����»���١��ơ�ϲ����Ϊ�Ƕ�
	};
	
	int m_cbCardDataArray_wuxier[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, // 0-8 һ��-����
			9, 10, 11, 12, 13, 14, 15, 16, 17, // 9-17 һͲ-��Ͳ
			18, 19, 20, 21, 22, 23, 24, 25, 26, // 18-26 һ��-����
			27, 28, 29, // �컨 ǧ�� �׻�

			0, 1, 2, 3, 4, 5, 6, 7, 8, // 0-8 һ��-����
			9, 10, 11, 12, 13, 14, 15, 16, 17, // 9-17 һͲ-��Ͳ
			18, 19, 20, 21, 22, 23, 24, 25, 26, // 18-26 һ��-����
			27, 28, 29, // �컨 ǧ�� �׻�

			0, 1, 2, 3, 4, 5, 6, 7, 8, // 0-8 һ��-����
			9, 10, 11, 12, 13, 14, 15, 16, 17, // 9-17 һͲ-��Ͳ
			18, 19, 20, 21, 22, 23, 24, 25, 26, // 18-26 һ��-����
			27, 28, 29, // �컨 ǧ�� �׻�

			0, 1, 2, 3, 4, 5, 6, 7, 8, // 0-8 һ��-����
			9, 10, 11, 12, 13, 14, 15, 16, 17, // 9-17 һͲ-��Ͳ
			18, 19, 20, 21, 22, 23, 24, 25, 26, // 18-26 һ��-����
			27, 28, 29, // �컨 ǧ�� �׻�
	};

	MajPaiQiang(boolean is_xier) {
		if(is_xier){
			m_byFetchPos = 0;
			max_card_num = 125;
			m_byMjPai = new int[max_card_num];
			memcpy(m_byMjPai, m_cbCardDataArray, sizeof(m_byMjPai));
		}else{
			m_byFetchPos = 0;
			max_card_num = 120;
			m_byMjPai = new int[max_card_num];
			memcpy(m_byMjPai, m_cbCardDataArray_wuxier, sizeof(m_byMjPai));
		}
		
	}

	void Ruffle(int in_RandCount, boolean is_xier) {
		if(is_xier){
			m_byFetchPos = 0;
			memcpy(m_byMjPai, m_cbCardDataArray, sizeof(m_byMjPai));

			int randseed1 = 0;
			int randseed2 = 0;
			for (int i = 0; i < in_RandCount; i++) {
				randseed1 += randseed2;
				randseed2 = Constants.rand();
				int randPos1 = randseed1 % max_card_num;
				int randPos2 = randseed2 % max_card_num;

				int by1, by2;
				by1 = m_byMjPai[randPos1];
				by2 = m_byMjPai[randPos2];
				m_byMjPai[randPos1] = by2;
				m_byMjPai[randPos2] = by1;
			}
		}else{
			m_byFetchPos = 0;
			memcpy(m_byMjPai, m_cbCardDataArray_wuxier, sizeof(m_byMjPai));

			int randseed1 = 0;
			int randseed2 = 0;
			for (int i = 0; i < in_RandCount; i++) {
				randseed1 += randseed2;
				randseed2 = Constants.rand();
				int randPos1 = randseed1 % max_card_num;
				int randPos2 = randseed2 % max_card_num;

				int by1, by2;
				by1 = m_byMjPai[randPos1];
				by2 = m_byMjPai[randPos2];
				m_byMjPai[randPos1] = by2;
				m_byMjPai[randPos2] = by1;
			}
		}
	}

	int GetPai() {
		if (m_byFetchPos < max_card_num) {
			int byPai = m_byMjPai[m_byFetchPos];
			if (byPai < 46 && byPai != Constants.INVALID_CARD) {
				m_byMjPai[m_byFetchPos] = Constants.INVALID_CARD;
				m_byFetchPos++;
				return byPai;
			} 
//			else if (byPai == Constants.INVALID_CARD) {
//				m_byFetchPos++;
//				return GetPai();
//			}
		}
		return Constants.INVALID_CARD;
	}

	/**
	 * ��ȡָ����
	 * 
	 * @param pai
	 * @return
	 */
	int GetPai(int pai) {
		for (int i = 0; i < max_card_num; i++) {
			if (m_byMjPai[i] == pai) {
				m_byMjPai[i] = Constants.INVALID_CARD;
				return pai;
			}
		}
		return Constants.INVALID_CARD;
	}

	int GetGangMoPai() // ��ȡ���Ƶ�����
	{
		return GetPai();
	}

	int GetBuHuaMoPai() // ��ȡ����������
	{
		return GetPai();
	}

	int GetFetchPosition() {
		return m_byFetchPos;
	}

	int IsEnd() {
		if (m_byFetchPos >= max_card_num) {
			return 1;
		}
		return 0;
	}

	// public static int PaiWei = 0;

	int GetYuPaiCount() {
		return (max_card_num - m_byFetchPos);
	}

	static int rand_inc = 0;

	int GetHuanPai(int byMj)// ��ʼʱ�Ļ���,��������ĵط���ʼȡ
	{
		rand_inc++;

		int randseed = rand() + rand_inc;

		int pos = 1;
		if (max_card_num - m_byFetchPos != 0) {
			pos = randseed % (max_card_num - m_byFetchPos);
		}
		if (pos == 0) {
			pos = 1;
		}
		int byHuanMj = m_byMjPai[max_card_num - pos];
		if (byHuanMj == byMj) {
			randseed = rand() + rand_inc;
			if (max_card_num - m_byFetchPos != 0) {
				pos = randseed % (max_card_num - m_byFetchPos);
			}
			if (pos == 0) {
				pos = 1;
			}
			byHuanMj = m_byMjPai[max_card_num - pos];
		}
		if (byHuanMj == byMj) {
			randseed = rand() + rand_inc;
			if (max_card_num - m_byFetchPos != 0) {
				pos = randseed % (max_card_num - m_byFetchPos);
			}
			if (pos == 0) {
				pos = 1;
			}
			byHuanMj = m_byMjPai[max_card_num - pos];
		}
		m_byMjPai[max_card_num - pos] = byMj;
		return byHuanMj;
	}
}

class S_PaiJu {

	int byFaPaiSeZi[] = new int[3];
	MajSide MajSides[] = new MajSide[4]; // ��������λ�ţ���������
	int byLastUser; // ���ֳ������
	int byLastOutMj; // �ϴγ���
	int byCurrentUser; // ��ǰ���
	int byZhuang; // ˭��ׯ
	int byLianZhuang; // ��ׯ��
	int byQuanFeng; // ��ǰ�ֵķ�λ

	void Init() {
		for (int i = 0; i < MajSides.length; i++) {
			MajSides[i] = new MajSide();
		}
		MajSides[0].Init();
		MajSides[1].Init();
		MajSides[2].Init();
		MajSides[3].Init();
		byLastUser = 0;
		byLastOutMj = 255;
		byCurrentUser = 0;
		byZhuang = 0;
		byLianZhuang = 0;
		byQuanFeng = 0;
	}
}

class S_JieSuan_Info {
	int lRoundScore;
	int bZiMo; // ÿ�κ��Ƿ�����

	void Init() {
		bZiMo = 0;
		lRoundScore = 0;
	}
};

class RGCPLogic {
	int max_card_num = 136;
	int m_byOutMj[] = new int[max_card_num];// ���ȥ��������ʾ����
	int m_byOutMjCount; // ����˶�������
	long m_wRound; // �ܾ�����ֻ��Ϊ�˼�¼��־��
	long m_wMaxRound; // �ܾ�����ֻ��Ϊ�˼�¼��־��
	int m_ActionRight[] = new int[4]; // ����Ȩ��,6-��,4-��,3-��,
	int m_byCurrentAction;
	S_PaiJu m_kPaiJu = new S_PaiJu();

	{
		m_kPaiJu.Init();
	}

	S_JieSuan_Info m_kJieSuan[] = new S_JieSuan_Info[4];

	{
		for (int i = 0; i < m_kJieSuan.length; i++) {
			m_kJieSuan[i] = new S_JieSuan_Info();
		}
	}

	RGCPLogic() {
		Init();
	}

	private void Init() {
		Init(Constants.INIT_TOTAL);
	}

	void Init(int byReason) {
		m_kPaiJu.byLastUser = Constants.INVALID_SEAT_VALUE;
		m_kPaiJu.MajSides[0].Init();
		m_kPaiJu.MajSides[1].Init();
		m_kPaiJu.MajSides[2].Init();
		m_kPaiJu.MajSides[3].Init();
		m_kPaiJu.byLastOutMj = Constants.INVALID_CARD;
		m_ActionRight[0] = 0;
		m_ActionRight[1] = 0;
		m_ActionRight[2] = 0;
		m_ActionRight[3] = 0;
		m_byCurrentAction = 0;
		if (Constants.INIT_TOTAL == byReason) {
			m_kPaiJu.byZhuang = 0;
			m_kPaiJu.byLianZhuang = 0;
			m_kPaiJu.byQuanFeng = 0;
			m_wRound = 0L;
			m_wMaxRound = 0L;
		}

		if (Constants.INIT_FIRST_ROUND == byReason) {
			m_kPaiJu.byZhuang = 0;
			m_kPaiJu.byLianZhuang = 0;
			m_kPaiJu.byQuanFeng = 0;
			m_wRound = 0L;
			m_wMaxRound = 0L;
		}
		if (Constants.INIT_NEWROUND == byReason) {
		}
		m_kPaiJu.byCurrentUser = m_kPaiJu.byZhuang;
		m_kJieSuan[0].Init();
		m_kJieSuan[1].Init();
		m_kJieSuan[2].Init();
		m_kJieSuan[3].Init();
		memset(m_byOutMj, Constants.INVALID_CARD, max_card_num);
		m_byOutMjCount = 0;
	}

	private void memset(int[] m_byOutMj2, int value, int length) {
		for (int i = 0; i < m_byOutMj2.length; i++) {
			m_byOutMj2[i] = value;
		}

	}

	boolean IsValidSeatID(int bySeatID) {
		if (bySeatID < RGCPTable.PLAYERCOUNT) {
			return true;
		}
		return false;
	}

	boolean IsNeedMoFeng(int in_Quan) {
		if (IsValidSeatID(m_kPaiJu.byZhuang) == false || GetRound() == 0) {
			return true;
		}
		return false;
	}

	void SetCurrentUser(int byCurrentUser) {
		m_kPaiJu.byCurrentUser = byCurrentUser;
	}

	void SetLastUser(int byLastUser) {
		m_kPaiJu.byLastUser = byLastUser;
	}

	void SetLastOutMj(int byLastOutMj) {
		m_kPaiJu.byLastOutMj = byLastOutMj;
	}

	void SetZhuang(int byZhuang) {
		m_kPaiJu.byZhuang = byZhuang;
	}

	void SetLianZhuang(int byLianZhuang) {
		m_kPaiJu.byLianZhuang = byLianZhuang;
	}

	void SetQuanFeng(int byQuanFeng) {
		m_kPaiJu.byQuanFeng = byQuanFeng;
	}

	void SetRound(long wRound) {
		m_wRound = wRound;
	}

	void SetMaxRound(long wRound) {
		m_wMaxRound = wRound;
	}

	void SetCurrentAction(int byAction) {
		m_byCurrentAction = byAction;
	}

	int GetCurrentAction() {
		return m_byCurrentAction;
	}

	int GetCurrentUser() {
		if (IsValidSeatID(m_kPaiJu.byCurrentUser) == false) {
			return 0;
		}
		return m_kPaiJu.byCurrentUser;
	}

	int GetLastUser() {
		if (IsValidSeatID(m_kPaiJu.byLastUser) == false) {
			return 0;
		}
		return m_kPaiJu.byLastUser;
	}

	int GetLastOutMj() {
		if (m_kPaiJu.byLastOutMj >= 42) {
			return Constants.INVALID_CARD;
		}
		return m_kPaiJu.byLastOutMj;
	}

	int GetZhuang() {
		if (IsValidSeatID(m_kPaiJu.byZhuang) == false) {
		}
		return m_kPaiJu.byZhuang;
	}

	int GetLianZhuang() {
		return m_kPaiJu.byLianZhuang;
	}

	int GetQuanFeng() {
		return m_kPaiJu.byQuanFeng;
	}

	long GetRound() {
		return m_wRound;
	}

	long GetMaxRound() {
		return m_wMaxRound;
	}

	public MajSide GetMajSide(int bySeatID) {
		return m_kPaiJu.MajSides[bySeatID];
	}
}

class S_MoFeng {
	int byMoFengSeZi[] = new int[3];
	int byFeng[] = new int[4];
	long dwSeatUserID[] = new long[4];

	void Init() {
	}
};
